<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* @framework : Codeigniter Framework
*
* @author: fatangel26
*
*/

	class Cart_model extends CI_Model
	{
		public function __construct()
		{
			parent::__construct();

            $this->load->database();

		}

        /*Basic Method*/


        public function delete()
        {

        }

        public function edit()
        {

        }

        /*Magic Method*/
        public function getNumsOrder()
        {
            /*
            $this->db->get_where("tbl_order", array('order_status' => 0));

            return $this->db->count_all_results();
            */

            $strSQL = "SELECT COUNT(*) as 'Nums' FROM tbl_order WHERE order_status = 0";

            return $this->db->query($strSQL)->row()->Nums;
        }

        public function getOrders()
        {
            $strSQL = "SELECT O.*,C.customer_name FROM tbl_order as O INNER JOIN tbl_customer as C ON O.customer_id = C.customer_id";

            $query = $this->db->query($strSQL);

            if($query->num_rows()>0)
                return $query->result_object();
            return FALSE;
        }

        public function updateorder($order_id, $order_status)
        {
            if(isset($order_status))
                $this->db->set('order_status', $order_status);

            $this->db->where('order_id', $order_id);

            if($this->db->update("tbl_order"))
                return TRUE;
            return FALSE;
        }

        public function delorder($order_id)
        {
            $query = $this->db->delete('tbl_order', array('order_id' => $order_id));

            if($this->db->affected_rows() > 0)
                return TRUE;
            return FALSE;
        }

        public function getorder($order_id)
        {
            $strSQL = "SELECT C.*,O1.* FROM tbl_customer AS C INNER JOIN tbl_order AS O1
                       ON C.customer_id = O1.customer_id
                       WHERE O1.order_id = ?";

            $query = $this->db->query($strSQL, $order_id);

            if($query->num_rows()>0)
                return $query->row();
            return FALSE;
        }

        public function get_order_items($order_id)
        {
            $strSQL = "SELECT O.*, P.product_name FROM tbl_order_item as O INNER JOIN tbl_product as P
                       ON O.product_id = P.product_id
                       WHERE O.order_id = ?";

            $query = $this->db->query($strSQL, $order_id);

            return $query->result_object();
        }

        public function updateship($data = array())
        {
            if($data['order_ship'] != 0)
            {
                $strSQL = "UPDATE tbl_order SET order_ship = order_ship + ? WHERE order_id = ?";
            }
            else
            {
                $data['order_ship'] = 0;
                $strSQL = "UPDATE tbl_order SET order_ship = ? WHERE order_id = ?";
            }

            $query = $this->db->query($strSQL, array($data['order_ship'], $data['order_id']));

            if($query)
            {
                $strSQL = "UPDATE tbl_order SET order_total = (order_ship + total) WHERE order_id = ?";

                $query = $this->db->query($strSQL, $data['order_id']);

                if($query)
                    return TRUE;
                return FALSE;
            }

            return FALSE;
        }

	}

/* End of file Pages_model.php */
/* Location: ./models/frontend/_model.php */